(module swapper (lib "eopl.ss" "eopl")
  (provide swapper)
  
;;1.17
;;swapper: s1 s2 slist ==> slist
;;usage: (swapper s1 s2 slist)
;;produces: Given 2 symbols,  and a list, this procedure produces another list with the symbols swapped in the result

;;examples:
;;(swapper 'a 'd '(a b c d) ==> (d b c a)
;;(swapper 'a 'd '(a d () c d)) ==> (d a () c a)
;;(swapper 'x 'y '((x) y (z (x)))) ==> ((y) x (z (y)))

(define swapper
  (lambda (s1 s2 slist)
    (cond 
      ((null? slist) ())
      ((list? (car slist)) (cons (swapper s1 s2 (car slist)) (swapper s1 s2 (cdr slist))))
      ((eq? s1 (car slist)) (cons s2 (swapper s1 s2 (cdr slist))))
      ((eq? s2 (car slist))(cons s1 (swapper s1 s2 (cdr slist))))
      (else (cons (car slist) (swapper s1 s2 (cdr slist))))
      )
    )
  )

;;Tests
(swapper 'a 'd '(a d b c))
(swapper 'a 'd '(a b c d))
(swapper 'a 'd '(a d () c d))
(swapper 'x 'y '((x) y (z (x))))

)